from django.shortcuts import render, HttpResponseRedirect
from django.http import JsonResponse
from django.views.generic import View

from assets.models import IDC
from assets.forms import IDCForm, IDCUpdateForm


class IDCView(View):
    def get(self, request):
        all_idc = IDC.objects.all()
        return render(request, 'assets/idc.html', {
            'all_idc': all_idc
        })
    



class IDCAddView(View):
    def get(self,request):
        return render(request, 'assets/idcadd.html', {})
    
    def post(self, request):
        idc_form = IDCForm(request.POST)
        if idc_form.is_valid():
            idc = idc_form.save(commit=True)
        return HttpResponseRedirect('/assets/idc/')


class IDCUpdateView(View):
    def get(self,request, idc_id):
        idc = IDC.objects.get(id=idc_id)
        return render(request, 'assets/idcupdate.html', {"idc":idc})
    
    def post(self, request, idc_id):
        idc = IDC.objects.get(id=idc_id)
        idc_form = IDCUpdateForm(request.POST, instance=idc)
        if idc_form.is_valid():
            idc_form.save(commit=True)
        return HttpResponseRedirect('/assets/idc/')


class IDCDelView(View):
    def get(self, request, idc_id):
        idc = IDC.objects.get(id=idc_id)
        idc.delete()
        return JsonResponse({
            'status': 200
        })
    
